package com.house.rent.ui.activity;

import android.Manifest;
import android.os.Bundle;
import android.os.Handler;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.base.lib.util.MaterialDialogUtils;
import com.house.rent.App;
import com.house.rent.R;
import com.tbruyelle.rxpermissions2.RxPermissions;


public class SplashActivity extends AppCompatActivity {

    /**
     * 6.0动态获取启动应用必须的权限
     */
    public static final String[] PERMISSIONS = new String[]{
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_splash);
        checkPermission();
    }

    protected void checkPermission() {
        new RxPermissions(this).request(PERMISSIONS)
                .subscribe(aBoolean -> {
                    if (aBoolean) {
                        //true表示获取权限成功（android6.0以下默认为true）
                        // 所有权限都已获取
                        new Handler().postDelayed(() -> {
                            if (App.getInstance().isLogin()) {
                                MainActivity.start(SplashActivity.this);
                            } else {
                                LoginActivity.start(SplashActivity.this);
                            }
                            finish();
                        }, 1500);
                    } else {
                        // 部分权限未获取
                        if (!isFinishing()) {
                            showDialog();
                        }
                    }
                });
    }

    @Override
    public void onBackPressed() {
        //屏蔽返回键
    }

    private void showDialog() {
        String msg = "为了保证您正常地使用APP,当我们获取您的存储空间等权限时，请点击允许，拒绝后APP将无法正常运行。\n\n"
                + "存储权限:用于APP版本升级\n"
                + "手机信息权限:用于第三方统计bug等\n";
        MaterialDialogUtils.show(this, msg, "退出", (dialog, which) -> finish(),
                "确定", (dialog, which) -> checkPermission());

    }

}
